Enumero los contactos del servicio web y lo muestro en los contactos 'seccionados' tableView como se ve en la captura de pantalla. El problema es que obtengo los mismos valores de etiqueta para las casillas de verificación de la primera fila de la sección A y la sección S. He ordenado una matriz y la he mostrado en la vista de tabla indexada. Cómo obtener diferentes valores de etiqueta según indexPath.row independientemente del número de secciones mostradas. Esto es lo que he intentado En cellForRowAtIndexPath: UIButton * checkBox; si (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone) { checkBox = [[UIButton alloc] initWithFrame: CGRectMake (7, 8, 30, 30)]; } más { checkBox = [[UIButton alloc] initWithFrame: CGRectMake (15, 13, 30, 30)]; } // int cnt = 0; // for (int i = indexPath.section - 1; i> 0; i--) // { // cnt + = [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: i]] count]; // arrayOfCharachters tiene char 'A' a 'Z' //} //checkBox.tag = cnt + indexPath.row; [checkBox setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal]; [checkBox addTarget: autoacción: @selector (checkBoxClicked :) forControlEvents: UIControlEventTouchUpInside]; [checkBox setTag: indexPath.row]; [cell.contentView addSubview: checkBox]; celda de retorno; } - (void) checkBoxClicked: (id) remitente { CGPoint buttonPosition = [remitente convertPoint: CGPointZero toView: self.tableViewContact]; NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint: buttonPosition]; UIButton * tappedButton = (UIButton *) remitente; NSLog (@ "Número de etiqueta =% d", [etiqueta del remitente]); if ([tappedButton.currentImage isEqual: [UIImage imageNamed: @ "checkBox.png"]]) { [Remitente setImage: [UIImage imageNamed: @ "checkBoxMarked.png"] forState: UIControlStateNormal]; si (indexPath! = Nil) { NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; // Identificaciones de los almacenes registran en mutableArrayOfIds NSLog (@ "ID de botón marcado con etiqueta =% @", finalIntId); [arrayOfIds addObject: finalIntId]; } // NSString * finalIntId = [mutableArrayOfIds objectAtIndex: tappedButton.tag]; // NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; } más { [Remitente setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal]; NSLog (@ "Sin comprobar"); // [arrayOfIds removeObjectAtIndex: tappedButton.tag]; } } - (NSString *) tableView: (UITableView *) tableView titleForHeaderInSection: (NSInteger) sección { if ([arrayOfCharacters count] == 0) { regreso @""; } return [NSString stringWithFormat: @ "% @", [arrayOfCharacters objectAtIndex: section]]; } - (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView { NSArray * toBeReturned = [NSArray arrayWithArray: [@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | # " componentsSeparatedByString: @ "|"]]; volver toBeReturned; } - (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) título enIndex: (NSInteger) índice { NSInteger recuento = 0; para (NSString * carácter en arrayOfCharacters) { if ([carácter esEqualToString: título]) { recuento de retorno; } contar ++; } return 0; } - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { return [recuento de arrayOfCharacters]; // devuelve 1; } - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: sección (NSInteger) { // return [cuenta mutableArray]; return [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: section]] count]; }
2020-12-07 22:56:16
está estableciendo el mismo valor de etiqueta para todas las secciones que tienen la misma fila. IndexPath tiene dos propiedades, {sección, fila}. Digamos que la sección A tiene dos filas, para row1 -> indexPath.section = 0, indexPath.row = 0; para row2-> indexPath.section = 0, indexPath.row = 1; Digamos que la sección S tiene dos filas, para row1 -> indexPath.section = 1, indexPath.row = 0; para row2-> indexPath.section = 1, indexPath.row = 1; Entonces, para la fila1 de la sección A y la fila1 de la sección S, está configurando el mismo valor de etiqueta que es 0. Ese es su problema. Intente configurar el valor de la etiqueta como se muestra a continuación. button.tag = indexPath.section * 1000 + indexPath.row; al recuperar la sección y la fila, Sección NSInteger = (button.tag) / 1000; NSInteger row = (button.tag)% 1000; | Prueba esto... - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { NSString estático * CellIdentifier = @ "Celda"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; si (celda == nulo) { celda = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } UIButton * checkBox = [[UIButton alloc] initWithFrame: CGRectMake (280, 10, 50, 44)]; checkBox.backgroundColor = [UIColor orangeColor]; [checkBox addTarget: autoacción: @selector (checkBoxClicked: event:) forControlEvents: UIControlEventTouchUpInside]; [checkBox setTag: indexPath.row]; [cell.contentView addSubview: checkBox]; celda de retorno; } - (void) checkBoxClicked: (id) evento del remitente: evento (id) { NSSet * toca = [evento allTouches]; UITouch * touch = [toca cualquier objeto]; CGPoint currentTouchPosition = [toque locationInView: self.tv]; // aquí tv es TableView Object NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition]; NSLog (@ "valor de indePath.section% d, indexPath.row% d", indexPath.section, indexPath.row); } | Esto sucede porque está asignando etiquetas a botones INDEPENDIENTES de secciones. Las dos primeras filas de las secciones A y S tienen fila = 0. por lo que la etiqueta asignada a su botón respectivo es 0. Debe asignarlas Manteniendo la referencia a sus secciones. Sugeriría asignar una sugerencia de accesibilidad con un formulario separado por comas que contenga Sección, Fila. Y en tu método - (void) checkBoxClicked: (id) remitente { // elige una cadena de la forma separada por comas. La primera es su sección, la segunda es la fila. } La segunda opción es Haga lo que esté haciendo e implemente su método Button de esta manera. CGPoint buttonPosition = [remitente convertPoint: CGPointZero toView: self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: buttonPosition]; si (indexPath! = nil) { ... indexpath.section es su sección, index path.row es su fila. } También hay una tercera opción. en cellforRowAtIndexpath, asigne un título a su botón - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath [btn setTitle: <# Your Row #> forState: UIControlStateDisabled]; [btn setTag <#Your Section #>]; por lo tanto, al recibir en su método de botón, puede tener tanto la sección (etiqueta) como la fila (título para el estado deshabilitado). - (void) checkBoxClicked: (id) sender {[button titleForState: UIControlStateDisabled]; // tu fila button.tag // tu sección} | ¡Prueba esto! Cada sección puede saber el recuento de la sección correcta y luego agregar el recuento de la fila individual. [[fieldTitlesList objectAtIndex: indexPath.section - 1] recuento] + indexPath.row Donde fieldTitlesList es la matriz de sus secciones. | Agregué el siguiente código que resolvió mi problema NSInteger rowNumber = 0; para (NSInteger i = 0; i